#include <stdio.h>
#include <omp.h>

#define SIZE 100

// func7 函数实现并行且平铺的操作
void func7(double A[SIZE][SIZE]) {
    #pragma omp parallel for schedule(static)
    #pragma omp tile sizes(4, 16)
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            A[i][j] = A[i][j] + 1;
        }
    }
}

// func8 函数处理并行和不完全平铺情况
void func8(double A[SIZE][SIZE]) {
    #pragma omp parallel for schedule(static)
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j += 16) {
            int end_j = (j + 16 > SIZE)? SIZE : j + 16;
            for (int k = j; k < end_j; k++) {
                A[i][k] = A[i][k] + 1;
            }
        }
    }
}

// func9 函数同样处理并行和不完全平铺
void func9(double A[SIZE][SIZE]) {
    #pragma omp parallel for schedule(static)
    for (int i = 0; i < SIZE; i += 4) {
        for (int j = 0; j < SIZE; j += 16) {
            int end_i = (i + 4 > SIZE)? SIZE : i + 4;
            int end_j = (j + 16 > SIZE)? SIZE : j + 16;
            for (int ii = i; ii < end_i; ii++) {
                for (int jj = j; jj < end_j; jj++) {
                    A[ii][jj] = A[ii][jj] + 1;
                }
            }
        }
    }
}

int main() {
    double A[SIZE][SIZE];
    // 初始化数组
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            A[i][j] = 0;
        }
    }

    // 调用 func7 函数
    func7(A);
    printf("After func7:\n");
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            printf("%f ", A[i][j]);
        }
        printf("\n");
    }

    // 重新初始化数组
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            A[i][j] = 0;
        }
    }

    // 调用 func8 函数
    func8(A);
    printf("After func8:\n");
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            printf("%f ", A[i][j]);
        }
        printf("\n");
    }

    // 再次重新初始化数组
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            A[i][j] = 0;
        }
    }

    // 调用 func9 函数
    func9(A);
    printf("After func9:\n");
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            printf("%f ", A[i][j]);
        }
        printf("\n");
    }

    return 0;
}    